package cn.jb698.codec.msm.generator.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class BeanUtils {

	// Bean --> Map 1: 利用Introspector和PropertyDescriptor 将Bean --> Map  
	/**
	 * 将Bean转换为Map.提取对象内的读取方法进行转换.
	 * @param obj
	 * @return
	 * @throws IntrospectionException 
	 * @throws InvocationTargetException 
	 * @throws IllegalArgumentException 
	 * @throws IllegalAccessException 
	 */
    public static Map<String, Object> transBean2Map(Object obj) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {  

        if(obj == null){  
            return null;  
        }
        Map<String, Object> map = new HashMap<String, Object>();  
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());  
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  
        for (PropertyDescriptor property : propertyDescriptors) {  
        	String key = property.getName();  
        	
        	// 过滤class属性  
        	if (!"class".equals(key)) {  
        		// 得到property对应的getter方法  
        		Method getter = property.getReadMethod();  
        		Object value = getter.invoke(obj);  
        		map.put(key, value);  
        	}  
        }  

        return map;  
    }
    
    /**
     * 将Map转换为Bean 
     * @param map
     * @param obj
     * @throws IntrospectionException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
     * @throws IllegalAccessException 
     */
    // Map --> Bean 1: 利用Introspector,PropertyDescriptor实现 Map --> Bean  
    public static void transMap2Bean(Map<String, Object> map, Object obj) throws IntrospectionException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {  

    	BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());  
    	PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();  

    	for (PropertyDescriptor property : propertyDescriptors) {  
    		String key = property.getName();  
    		
    		if (map.containsKey(key)) {  
    			Object value = map.get(key);  
    			// 得到property对应的setter方法  
    			Method setter = property.getWriteMethod();  
    			setter.invoke(obj, value);
    		}  
    	}
        return;  
    }  
    
    
    /**
     * 传统方式转换Bean到Map
     * @param inObj
     * @param ignoreProps
     * @return
     */
    public static Map<String,Object> transBeanToMap(Object inObj,String ...ignoreProps ){
		Map<String,Object> rtMap = null ;
		if(inObj == null ){
			return rtMap ;
		}
		rtMap = new HashMap<String, Object>();
		Method[] methods = inObj.getClass().getMethods();
		for(Method method : methods ){
			String methodName = method.getName();
			if(methodName.startsWith("get") && methodName.length() > 3 ){ // 取get方法.
				String paramName = methodName.substring(3, 4).toLowerCase() + methodName.substring(4);
//				System.err.println("方法名称=" + methodName);
//				System.err.println("对应参数名称=" + paramName);
				// 判断是否在忽略名单里,如果是,则跳过.
				boolean isIgnoreParam = false ;
				for(String ignProps : ignoreProps ){
					if( ignProps.equals(paramName)){
						isIgnoreParam = true ;
						break ;
					}
				}
				if(isIgnoreParam){
					continue ;
				}
				if("class".equals(paramName)){ // class 的也跳过 
					continue;
				}

				try {
					Object value = method.invoke(inObj);
					if(value != null){
						rtMap.put(paramName, value);
					}
//					System.out.println("value=" + value);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return rtMap ;
	}
    
}
